<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回" @clickRight="backWork" right-icon="home"
			:title = this.titleName @clickLeft="back" />

		<uni-table ref="table" border stripe>
				<uni-tr>
					<uni-th width="80" align="center">类别</uni-th>
					<uni-th width="60" align="center">净售量</uni-th>
					<!-- <uni-th width="40" align="center">赠品量</uni-th> -->
					<uni-th width="100" align="center">净售额</uni-th>
					<uni-th width="100" align="center">净成本</uni-th>
					<uni-th width="100" align="center">商品利润</uni-th>
					<uni-th width="60" align="center">操作</uni-th>
				</uni-tr>
			
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td align="center">{{ item.categoryName }}</uni-td>
				<uni-td align="center">{{ item.netSalesVolume }}</uni-td>
				<!-- <uni-td align="center">{{ item.giftQuantity }}</uni-td> -->
				<uni-td align="center">{{ item.netSalesAmount }}</uni-td>
				<uni-td align="center">{{ item.netCost }}</uni-td>
				<uni-td align="center">{{item.profit}}</uni-td>	
				<uni-td align="center">
					<view class="uni-group">
						<uni-icons type="redo" size="25" @click="details(item.categoryId,item.categoryName)"></uni-icons>
						<!-- <button size="mini" type="primary" @click="details(item.brandId)">详情</button> -->
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>



	</view>
</template>

<script>
	import {baseUrl} from 'libs/util/global'
	export default {
		data() {
			return {
				loading: false,
				boxHeight: 0, // box盒子的高度
				year: '',
				quanter: '',
				title: '',
				chainStore: '',
				handledBy: '',
				startTime: '',
				endTime: '',
				loginUserId: null,
				loginUserType: null,
				titleYear: '',
				titleQuanter: '',
				authority: null,
				chainStoreName: null,
				titleName: '',
				tableData: []
			}
		},

		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log("========品牌页面=====")
			console.log(option.year); //打印出上个页面传递的参数。
			this.year = option.year
			this.titleYear = option.titleYear
			this.titleQuanter = option.titleQuanter
			console.log(option.titleQuanter)
			this.chainStoreName = option.chainStoreName
			console.log(option.chainStoreName+"店铺名称")
			console.log(option.quanter); //打印出上个页面传递的参数。
			this.quanter = option.quanter
			console.log("信息："+option.chainStore); //打印出上个页面传递的参数。
			this.chainStore = option.chainStore
			console.log(option.handledBy) //经手人信息
			this.handledBy = option.handledBy
			this.title = option.titleYear + option.titleQuanter
			console.log(option.startTime)
			this.startTime = option.startTime
			console.log(option.endTime)
			this.endTime = option.endTime
			uni.getStorage({
				key: 'userId',
				success:(res)=> {
					// console.log(res.data + "登录用户id:"+res.data)
					this.loginUserId = res.data
				}
			});
			uni.getStorage({
				key: 'isChain',
				success:(res) =>{
					// console.log(res.data + "登录用户类型:"+res.data)
					this.loginUserType = res.data
				}
			});
			uni.getStorage({
				key: 'authority',
				success:(res) =>{
					// console.log(res.data + "登录用户权限:"+res.data)
					this.authority  = res.data
				}
			});
		},
		mounted() {
			this.getInfo(),
			this.getBoxHeight(),
			this.initTitleName()
		},
		methods: {
			initTitleName(){
				if(this.chainStore === ""){
					this.titleName = this.titleYear + this.titleQuanter
				}else{
					console.log("进入有名称店铺")
					this.titleName = this.chainStoreName.substring(0,6) + " "+ this.titleYear + " "+ this.titleQuanter
				}
			},


			//获取盒子高度
			getBoxHeight() {
				uni.createSelectorQuery()
					.in(this)
					.select('.table-header')
					.boundingClientRect(rect => {
						if (rect) {
							this.boxHeight = rect.height;
						}
					})
					.exec();
			},

			// 左上角返回按钮
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			backWork(){
				uni.switchTab({
					url: '/pages/work/work'
					})
			},
			// 获取信息
			getInfo() {
				let time = this.year + this.quanter
				uni.showLoading({
						title: "加载中...."
					}),
				uni.request({
					url: baseUrl+'/sales/category',
					header: {
						'content-type': 'application/json'
					},
					method: 'POST',
					data: {
						userId: this.loginUserId,
						userType: this.loginUserType,
						chainStore: this.chainStore,
						handledBy: this.handledBy,
						yearId: this.year,
						quarterId: this.quanter,
						startTime:this.startTime,
						endTime: this.endTime
					},
					success: (res) => {
						if (res.data.code == 200) {
							setTimeout(function() {
									uni.hideLoading();
								}, 200)
								if(this.authority == 2){
									
									for (var i = 0; i < res.data.data.length; i++) {
										res.data.data[i].netCost = '0.00'
										res.data.data[i].profit = '0.00'
									}
									
									this.tableData = res.data.data
								}else{
									this.tableData = res.data.data
								}
							this.tableData = res.data.data
						} else {

						}
					}
				})
			},
			// 进入详情
			details(categoryId,categoryName) {
				console.log(this.titleQuanter)
				console.log(categoryName)
				// let time = this.time;
				let chainStore = this.chainStore;
				let handledBy = this.handledBy;
				let startTime = this.startTime
				console.log("======开始时间：" + startTime)
				let endTime = this.endTime
				console.log("======结束时间：" + endTime)
				uni.navigateTo({
					url: '/pages/form/quanterSales/category/categoryDetails/categoryDetails?year=' + this.year +
					 '&quanter='+this.quanter + 
					 '&chainStore=' +chainStore + 
					 '&handledBy=' + handledBy + 
					 '&categoryId=' + categoryId  + 
					 '&startTime=' + startTime +
					 '&endTime='+endTime + 
					 '&titleYear=' + this.titleYear +
					 '&titleQuarter=' + this.titleQuanter +
					 '&categoryName='+ categoryName +
					 '&chainStoreName=' + this.chainStoreName
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.content {
			width: 100%;
			height: 100vh;
		}

	.table-header {
		position: sticky;
		left: 0;
		top: 0;
		z-index: 99999;
		background-color: #fff;
	}

	//冻结表头第一列
	/deep/ .uni-table-tr .uni-table-td:first-child {
		position: sticky;
		left: 0;
		top: 0;
		background-color: lavenderblush;
		z-index: 0;
	}

	/deep/ .uni-table-tr {
		overflow: visible;
		background-color: #fff;
	}

	/deep/ .uni-table-tr .uni-table-th:first-child {
		position: sticky;
		left: 0;
		top: 0;
		background-color: #fff;
		z-index: 0;
	}
</style>